Completed
Push — master ( d362a9...56faa4 )
by Mark
14s queued 11s
created

Field   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 103
dl 0
loc 136
rs 10
c 0
b 0
f 0
wmc 18

9 Functions

Rating   Name   Duplication   Size   Complexity  
A resetDirty 0 4 1
A addParentOriginalValueLookUp 0 6 1
A setFillPropertyOnParent 0 12 4
A setName 0 3 1
A toJson 0 5 1
A tableSetup 0 3 1
A setParentProperties 0 7 1
A setup 0 4 1
B addParentFieldValueLookUp 0 19 7
1
import {ModelInterface, TableInterface} from "../../JeloquentInterfaces";
2
3
export default class Field {
4
5
    protected $fieldValue: unknown;
6
7
    protected $name: string;
8
9
    protected $originalValue: unknown;
10
11
    protected $parent: ModelInterface;
12
13
    protected $previousValue: unknown;
14
15
    private _isPrimary: boolean;
16
17
    /**
18
     *
19
     * @param name
20
     * @param isPrimary
21
     */
22
    constructor(name: string, isPrimary = false) {
23
        this._isPrimary = isPrimary;
24
        this.$name = name;
25
        this.$fieldValue = null;
26
        this.$previousValue = undefined;
27
        this.$originalValue = undefined;
28
        this.$parent = null;
29
    }
30
31
    get isDirty(): boolean {
32
        return this.$fieldValue != this.$previousValue;
33
    }
34
35
    get isPrimary(): boolean {
36
        return this._isPrimary;
37
    }
38
39
    get name(): string {
40
        return this.$name;
41
    }
42
43
    get originalValue(): unknown {
44
        return this.$originalValue;
45
    }
46
47
    get previousValue(): unknown {
48
        return this.$previousValue;
49
    }
50
51
    get value(): unknown {
52
        return this.$fieldValue;
53
    }
54
55
    set value(value: unknown) {
56
57
        this.$fieldValue = value;
58
    }
59
60
    resetDirty(): void {
61
        this.$originalValue = JSON.parse(JSON.stringify(this.$fieldValue));
62
        this.$previousValue = JSON.parse(JSON.stringify(this.$fieldValue));
63
    }
64
65
    setName(): Field {
66
        return this;
67
    }
68
69
    setup(parent: ModelInterface): Field {
70
        this.$parent = parent;
71
        return this.setName().setParentProperties();
72
    }
73
74
    tableSetup(table: TableInterface): void {
75
        console.info(table.name);
76
    }
77
78
    toJson(): object {
79
        const object = {};
80
        object[this.$name] = this.value;
81
        return JSON.parse(JSON.stringify(object));
82
    }
83
84
    protected addParentFieldValueLookUp(): void {
85
        Object.defineProperty(this.$parent,
86
            this.$name, {
87
                get: () => {
88
                    return this.value;
89
                },
90
                set: (value) => {
91
                    if (this.$previousValue === undefined) {
92
                        this.$previousValue = JSON.parse(JSON.stringify(this.value ?? value));
93
                    }
94
95
                    if (this.$originalValue === undefined) {
96
                        this.$originalValue = JSON.parse(JSON.stringify(this.value ?? value));
97
                    }
98
99
                    this.$previousValue = JSON.parse(JSON.stringify(this.value));
100
101
                    this.value = value;
102
                }
103
            }
104
        )
105
    }
106
107
    protected addParentOriginalValueLookUp(): void {
108
        Object.defineProperty(this.$parent,
109
            `original_${this.$name}`, {
110
                get: () => {
111
                    return this.originalValue;
112
                },
113
            }
114
        )
115
    }
116
117
    protected setFillPropertyOnParent(): void {
118
        Object.defineProperty(this.$parent,
119
            `_${this.$name}`,
120
            {
121
                set: (value) => {
122
                    if (this.$originalValue === undefined) {
123
                        this.$originalValue = JSON.parse(JSON.stringify(this.value ?? value));
124
                    }
125
126
                    this.$previousValue = JSON.parse(JSON.stringify(this.value));
127
                    this.value = value;
128
                }
129
            });
130
    }
131
132
    protected setParentProperties(): Field {
133
        this.addParentFieldValueLookUp();
134
        this.addParentOriginalValueLookUp();
135
        this.setFillPropertyOnParent();
136
137
        return this;
138
    }
139
}